package com.lzx.adx.report.util;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MacroReplacement {

    /**
     * 替换字符串中的宏
     *
     * @param input  待处理的字符串
     * @param macros 宏及其对应的值
     * @return 替换后的字符串
     */
    public static String replaceMacros(String input, Map<String, String> macros) {
        StringBuilder sb = new StringBuilder(input);
        Pattern pattern = Pattern.compile("__([\\w-]+)__");
        Matcher matcher = pattern.matcher(sb);
        int offset = 0;
        while (matcher.find()) {
            String macro = matcher.group(1);
            String value = macros.get(macro);
            if (value != null) {
                sb.replace(matcher.start() , matcher.end() , value);
                int diff=matcher.end()-matcher.start();
                int start;
                if(value.length()==diff){
                    start=matcher.end();
                }else if(value.length()>diff){
                    start=matcher.end()+ (value.length()-diff);
                }else {
                    start=matcher.end()-(diff-value.length());
                }
                matcher.region(start, sb.length());
            }
        }
        return sb.toString();
    }



    /**
     * 替换字符串中的宏
     *
     * @param input  待处理的字符串
     * @param macros 宏及其对应的值
     * @return 替换后的字符串
     */
    public static String replaceMacros2(String input, Map<String, String> macros) {
        StringBuilder sb = new StringBuilder(input);
        Pattern pattern = Pattern.compile("\\{([\\w-]+)\\}");
        Matcher matcher = pattern.matcher(sb);
        int offset = 0;
        while (matcher.find()) {
            String macro = matcher.group(1);
            String value = macros.get(macro);
            if (value != null) {
                sb.replace(matcher.start() , matcher.end() , value);
                int diff=matcher.end()-matcher.start();
                int start;
                if(value.length()==diff){
                    start=matcher.end();
                }else if(value.length()>diff){
                   start=matcher.end()+ (value.length()-diff);
                }else {
                    start=matcher.end()-(diff-value.length());
                }
                matcher.region(start, sb.length());
            }
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        String url="http://wzjblog.gnway.cc/ad/data/report?channel=hw&tagId=xx&content_id={content_id}&adgroup_id={adgroup_id}&campaign_id={campaign_id}&oaid={oaid}&trace_time={trace_time}&callback={callback}&corp_id={corp_id}&app_id={app_id}&event_type={event_type}&ip={ip}&user_agent={user_agent}&requestId={transunique_id}";

        Map<String,String> params=new HashMap<>();
        params.put("corp_id","1");
        params.put("content_id","1");
        params.put("adgroup_id","2");
        params.put("campaign_id","3");
        params.put("oaid","132133");
        params.put("trace_time",String.valueOf(System.currentTimeMillis()));
        params.put("callback", URLEncoder.encode("{\"a\":\"AAA\"}"));
        params.put("app_id","4");
        params.put("event_type","5");
        params.put("ip","192.168.1.1.1");
        params.put("user_agent","24324324324324234");
        params.put("transunique_id","24324999999999");


        String s = replaceMacros2( url, params);
        System.out.println(s);
    }
}